package com.wnzt.baszh.core.base;


import java.io.Serializable;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.wnzt.baszh.util.common.ConstantsUtils;
import com.wnzt.baszh.util.common.GlobalStatic;

/**
 * 查询条件对象
 * 
 * @version [版本�? 2013-6-28]
 * @see [相关�?方法]
 * @since [产品/模块版本]
 */
public class QueryObject implements Serializable {

    /**
     * 注释
     */
    private static final long serialVersionUID = 5098727654503871431L;

    /**
     * 查询语句，HQL及SQL及Procedures(存储过程名称)
     */
    private String queryString;
    
    /**
     * 查询count语句
     * */
    private String queryCountString;

    /**
     * 参数�?
     */
    private Object[] values;

    /**
     * 当前页数
     */
    private int curPage;

    /**
     * 每一页的记录�?
     */
    private int pageSize;

    /**
     * 条件对象
     */
    private DetachedCriteria detachedCriteria;

    /**
     * 默认构�?函数
     */
    public QueryObject() {
    }

    /**
     * @return the queryCountString
     */
    public String getQueryCountString() {
        return queryCountString;
    }

    /**
     * @param queryCountString the queryCountString to set
     */
    public void setQueryCountString(String queryCountString) {
        this.queryCountString = queryCountString;
    }



    public QueryObject(String queryString) {
        this.queryString = queryString;
    }

    public QueryObject(int curPage, int pageSize) {
        this.curPage = curPage;
        this.pageSize = pageSize;
    }

    public QueryObject(DetachedCriteria detachedCriteria, int curPage, int pageSize) {
        this.setDetachedCriteria(detachedCriteria);
        this.curPage = curPage;
        this.pageSize = pageSize;
    }

    /**
     * map.key:sort/order/curPage/pageSize
     * 
     */
    public QueryObject(DetachedCriteria detachedCriteria, Map<String, Object> map) {
        if (ConstantsUtils.isNotEmpty(map)) {
            // 当前�?
            int cPage = Integer.valueOf(
                    (null == map.get(GlobalStatic.QUERY_CONDITION_CUR_PAGE)) ? String.valueOf(GlobalStatic.INT_VALUE_1) : map.get(
                            GlobalStatic.QUERY_CONDITION_CUR_PAGE).toString()).intValue();
            // 每页显示的行�?
            int pSize = Integer.valueOf(
                    (null == map.get(GlobalStatic.QUERY_CONDITION_PAGE_SIZE)) ? String.valueOf(GlobalStatic.PAGE_SIZE) : map.get(
                            GlobalStatic.QUERY_CONDITION_PAGE_SIZE).toString()).intValue();
            // 排序字段
            if (null != map.get("sort") && null != map.get("order")) {
                String sort = (String) map.get("sort");
                String order = (String) map.get("order");
                // 排序方式
                if (ConstantsUtils.isNotBank(sort) && ConstantsUtils.isNotBank(order)) {
                    if ("desc".equals(order.toLowerCase())) {
                        detachedCriteria.addOrder(Order.desc(sort));
                    } else {
                        detachedCriteria.addOrder(Order.asc(sort));
                    }
                }
            }
            this.curPage = cPage;
            this.pageSize = pSize;
        }
        this.setDetachedCriteria(detachedCriteria);
    }

    public QueryObject(String hql, Map<String, Object> map) {
        if (ConstantsUtils.isNotEmpty(map)) {
            // 当前�?
            int cPage = Integer.valueOf(
                    (null == map.get(GlobalStatic.QUERY_CONDITION_CUR_PAGE)) ? String.valueOf(GlobalStatic.INT_VALUE_1) : map.get(
                            GlobalStatic.QUERY_CONDITION_CUR_PAGE).toString()).intValue();
            // 每页显示的行�?
            int pSize = Integer.valueOf(
                    (null == map.get(GlobalStatic.QUERY_CONDITION_PAGE_SIZE)) ? String.valueOf(GlobalStatic.PAGE_SIZE) : map.get(
                            GlobalStatic.QUERY_CONDITION_PAGE_SIZE).toString()).intValue();
            this.curPage = cPage;
            this.pageSize = pSize;
            this.queryString=hql;
        }
    }

    /**
     * 重载构�?方法
     * 
     * @param queryString
     *            SQL、SQL语句
     * @param values
     *            参数�?
     * @param curPage
     *            当前�?
     * @param pageSize
     *            每页大小
     */
    public QueryObject(String queryString, Object[] values, int curPage, int pageSize) {
        this.queryString = queryString;
        this.curPage = curPage;
        this.pageSize = pageSize;
    }

    /**
     * 重载构�?方法
     * 
     * @param queryString
     *            HQL、SQL语句
     * @param values
     *            参数�?
     */
    public QueryObject(String queryString, Object[] values) {
        this.queryString = queryString;
        this.values = values;
    }

    /**
     * 重载构�?方法
     * 
     * @param queryString
     *            查询语句，可以是HQL、SQL
     * @param curPage
     *            当前�?
     * @param pageSize
     *            每页大小
     */
    public QueryObject(String queryString, int curPage, int pageSize) {
        this.queryString = queryString;
        this.curPage = curPage;
        this.pageSize = pageSize;
    }

    /**
     * @return the values
     */
    public Object[] getValues() {
        return values;
    }

    /**
     * @param values
     *            the values to set
     */
    public void setValues(Object[] values) {
        this.values = values;
    }

    /**
     * @return the curPage
     */
    public int getCurPage() {
        return curPage;
    }

    /**
     * @param curPage
     *            the curPage to set
     */
    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize
     *            the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @param detachedCriteria
     *            the detachedCriteria to set
     */
    public void setDetachedCriteria(DetachedCriteria detachedCriteria) {
        this.detachedCriteria = detachedCriteria;
    }

    /**
     * @return the detachedCriteria
     */
    public DetachedCriteria getDetachedCriteria() {
        return detachedCriteria;
    }

    /**
     * @param queryString
     *            the queryString to set
     */
    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    /**
     * @return the queryString
     */
    public String getQueryString() {
        return queryString;
    }
}

